/*
 * date: 2009-11-20 
 * author: Maciej Smolenski <jezdnia@gmail.com>
 */
#include "rpc_config_data.hpp"

namespace rpc_config
{

rpc_host_entry_string::rpc_host_entry_string(const std::string p_address, const long p_port, const protocol_t p_protocol) : rpc_host_entry<std::string>(p_address, p_port, p_protocol) {};

rpc_host_entry_string::rpc_host_entry_string(const rpc_host_entry_shm_string &hes) : rpc_host_entry<std::string>(hes.address.c_str(), hes.port, hes.protocol) {};

rpc_host_entry_shm_string::rpc_host_entry_shm_string(const shm_string p_address, const long p_port, const protocol_t p_protocol) : rpc_host_entry<shm_string>(p_address, p_port, p_protocol) {};

rpc_host_entry_shm_string::rpc_host_entry_shm_string(const rpc_host_entry_string &hes, shm_char_allocator &al) : rpc_host_entry<shm_string>( shm_string(hes.address.c_str(), al), hes.port, hes.protocol) {};	

result_t std_set_to_bi_set(bi::managed_shared_memory &segment, const stdset &si, biset &so)
{
	shm_char_allocator c_allocator(segment.get_segment_manager());

	so.clear();
	for(stdset::const_iterator si_it=si.begin(); si_it!=si.end(); si_it++)
		so.insert(rpc_host_entry_shm_string(*si_it, c_allocator));
	return result_success;
}

result_t bi_set_to_std_set(bi::managed_shared_memory &segment, const biset &si, stdset &so)
{
	so.clear();
	for(biset::const_iterator si_it=si.begin(); si_it!=si.end(); si_it++)
		so.insert(rpc_host_entry_string(*si_it));
	return result_success;
}

result_t rpcm_config::init(bi::managed_shared_memory &m_segment)
{
	shm_rpc_host_entry_shm_string_allocator s_allocator(m_segment.get_segment_manager());
	m_hosts = m_segment.construct<biset>(bi::anonymous_instance)(std::less<rpc_host_entry_shm_string>(), s_allocator);
	m_delay = 10;
	m_ping_timeout = 1;
	m_portmap_timeout = 10;
	m_pgroup = 0;
	return result_success;
}

result_t rpcm_config::deinit(bi::managed_shared_memory &m_segment)
{
	m_hosts->clear();
	m_segment.destroy_ptr(m_hosts.get());
	return result_success;
}

std::ostream &operator<<(std::ostream &os, const rpc_host_entry_string &r)
{
	os << " address: " << r.address;
	os << " port: " << r.port;
	os << " protocol: " << r.protocol;
	return os;
}

}

